package cm.util.action.list;

//import ips.operate_log.dao.OplogDAO;
//import ips.util.ClassNameTrans.ClassNameTrans;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cm.util.action.info.GenericInfoActionSupport;
import cm.util.service.GenericService;
import cm.util.service.GenericServiceImpl;

/**
 * 
 * @author 苏玮
 *
 * 2010-11-29
 *
 * @param <E>
 * @param <PK>
 */
public class GenericListActionSupport<E> extends ListActionSupport<E>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8555589202087459230L;
	private static Log log=LogFactory.getLog(GenericListActionSupport.class);
	//private OplogDAO op=new OplogDAO();
	GenericService<E> service;
	//private ClassNameTrans classNameTrans = new ClassNameTrans();
	public E entity;
	/**
	 * 根据实体类类对象构造Action
	 * 
	 * @param entityClass
	 * 			实体类类对象
	 */
	public GenericListActionSupport(Class<E> entityClass){
		this(entityClass.getName());
		service=new GenericServiceImpl<E>(entityClass);
	}
	
	/**
	 * 根据实体类对象以及service方法构造Action
	 * @param entityClass
	 * @param service
	 */
	public GenericListActionSupport(Class<E> entityClass,GenericService<E> service){
		this.service=service;
	}
	
	/**
	 * 根据实体类类类名构造Action
	 * @param entityClassName
	 * 			实体类类名
	 */
	@SuppressWarnings("unchecked")
	public GenericListActionSupport(String entityClassName){
		try{
			/**
			 * 初始化实体对象
			 */
			entity=(E)Class.forName(entityClassName).newInstance();
			
		} catch (ClassNotFoundException e){
			log.error("不存在名为:"+entityClassName+"的类！", e);
		} catch (InstantiationException e) {
			log.error("实例化"+entityClassName+"对象出错！", e);
		} catch (IllegalAccessException e) {
			log.error(e);
		}
		service=new GenericServiceImpl<E>(entityClassName);
	}
	@Override
	protected String findAll() {
		totalRows=service.sizeAll();
		initPageProperties();
		//op.save("查看了模块",entity.getClass().getName(), "的详细信息");		//自己加的------------------
		list=service.findAll(currentPage, pageSize);
		return FIND_ALL_SUCCESS;
	}
	protected String findAllDesc() {
		totalRows=service.sizeAll();
		initPageProperties();
	//	System.out.print("无this:"+entity.getClass().getName());
	//	System.out.print("有this:"+this.entity.getClass().getName());
	//	op.save("查看了模块",classNameTrans.classNameTrans(this.entity.getClass().getName()), "的详细信息");		//自己加的------------------
		list=service.findAllDesc("desc",currentPage, pageSize);
		return FIND_ALL_DESC_SUCCESS;
	}
	protected String findAllDescWithDate(String beginDate,String endDate) {
		totalRows=service.sizeAll(beginDate,endDate);
		initPageProperties();
		//op.save("查看了模块",classNameTrans.classNameTrans(this.entity.getClass().getName()), "的详细信息");		//自己加的------------------
		list=service.findAllDescWithDate("desc",currentPage, pageSize,beginDate,endDate);
		return FIND_ALL_DESC_WITH_DATE_SUCCESS;
	}
	protected String findAllDescWithKeyWord(String keyWord){
		totalRows=service.sizeAll(keyWord);
		initPageProperties();
		list=service.findAllDescWithKeyWord("desc",currentPage, pageSize,keyWord);
		return FIND_ALL_DESC_WITH_KEYWORD_SUCCESS;
	}
}
